package pers.amos.eshop.advance.inventory.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import pers.amos.eshop.advance.inventory.model.ProductInventory;
import pers.amos.eshop.advance.inventory.service.ProductInventoryService;


@RestController
@RequestMapping("/product-inventory")
public class ProductInventoryController {

    @Autowired
    private ProductInventoryService productInventoryService;

    @RequestMapping("/add")
    @ResponseBody
    public String add(ProductInventory productInventory) {
        try {
            productInventoryService.add(productInventory);
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(ProductInventory productInventory) {
        try {
            productInventoryService.update(productInventory);
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(Long id) {
        try {
            productInventoryService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
        return "success";
    }

    @GetMapping("/findById")
    public ProductInventory findById(Long id) {
        try {
            return productInventoryService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ProductInventory();
    }

    /**
     * 根据商品 id 查询商品的库存 该接口暴露给 one service 系统
     * @param productId 商品id
     * @return
     */
    @GetMapping("/findByProductId")
    public ProductInventory findByProductId(Long productId) {
        try {
            return productInventoryService.findByProductId(productId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ProductInventory();
    }

}
